﻿namespace UFIDA.U9.Cust.U9Demo.DeliveryBP
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// CreateRcv partial 
    /// </summary>	
    public partial class CreateRcv
    {
        internal BaseStrategy Select()
        {
            return new CreateRcvImpementStrategy();
        }
    }

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
    internal partial class CreateRcvImpementStrategy : BaseStrategy
    {
        public CreateRcvImpementStrategy() { }

        public override object Do(object obj)
        {
            CreateRcv bpObj = (CreateRcv)obj;

            //get business operation context is as follows
            //IContext context = ContextManager.Context	

            //auto generating code end,underside is user custom code
            //and if you Implement replace this Exception Code...
            if (bpObj == null)
                return null;
            if(bpObj.DeliveryLineDTOList.Count == 1)
                CreateARApplyHeadExtend.CreateARApplyHead();
            else if(bpObj.DeliveryLineDTOList.Count > 1)
                CreateARApplyHeadExtend.CreateARDiffCurApplyHead();
            long rcvID = 0;
            //if (bpObj.DeliveryLineDTOList == null || bpObj.DeliveryLineDTOList.Count == 0)
            //    throw new Exception("转单数据不能为空");
            //UFIDA.U9.ISV.RCV.Proxy.CreateRCVSRVProxy proxy = new ISV.RCV.Proxy.CreateRCVSRVProxy();
            //proxy.RCVList = new List<ISV.RCV.DTO.OBAReceivementDTOData>();
            //ISV.RCV.DTO.OBAReceivementDTOData headData = new ISV.RCV.DTO.OBAReceivementDTOData();
            //headData.RcvDocType = new PM.DTOs.BESimp4UIDTOData();
            //headData.RcvDocType.Code = GetDefaultRMADocType();//可以用参数设置设置一个默认转单的收货单单据类型，或者指定一个单据类型编码
            //headData.BusinessDate = UFIDA.U9.Base.Context.OperationDate;
            //headData.RcvLines = new List<ISV.RCV.DTO.OBARcvLineDTOData>();
            //using (ISession session = Session.Open())
            //{
            //    string supplierCode = "";//供应商编码
            //    foreach (DeliveryLineDTO deliveryLineDTO in bpObj.DeliveryLineDTOList)
            //    {
            //        DeliverBE.DeliveryLine deliveryLine = deliveryLineDTO.DeliveryLineID.GetEntity();
            //        if (supplierCode == "")
            //        {
            //            supplierCode = deliveryLine.Delivery.Supplier.Code;
            //        }

            //        ISV.RCV.DTO.OBARcvLineDTOData lineDTO = new ISV.RCV.DTO.OBARcvLineDTOData();
            //        lineDTO.DocLineNo = deliveryLine.DocLineNo;
            //        lineDTO.ItemInfo = new CBO.SCM.Item.ItemInfoData();
            //        lineDTO.ItemInfo.ItemCode = deliveryLine.Item.Code;
            //        lineDTO.ArriveQtyTU = deliveryLineDTO.ToQty;
            //        lineDTO.FinallyPriceTC = deliveryLine.Price;
            //        lineDTO.DescFlexSegments = new Base.FlexField.DescFlexField.DescFlexSegmentsData();
            //        lineDTO.DescFlexSegments.PrivateDescSeg2 = deliveryLine.Delivery.DocNo;
            //        lineDTO.DescFlexSegments.PrivateDescSeg3 = deliveryLine.DocLineNo.ToString();
            //        deliveryLine.ArrivedQty += deliveryLineDTO.ToQty;
            //        headData.RcvLines.Add(lineDTO);

            //    }
            //    headData.Supplier = new CBO.SCM.Supplier.SupplierMISCInfoData();
            //    headData.Supplier.Code = supplierCode;
            //    proxy.RCVList.Add(headData);
            //    List<PM.Rcv.ReceivementData> rcvResultList = proxy.Do();
            //    foreach (PM.Rcv.ReceivementData rcvData in rcvResultList)
            //    {
            //        rcvID = rcvData.ID;
            //    }
            //    session.Commit();
            //}
            return rcvID;
        }

        public static string GetDefaultRMADocType()
        {
            string rcvDocTypeCode = "RCV01";
            UFIDA.U9.Base.Profile.Proxy.GetProfileValueProxy bpObj = new UFIDA.U9.Base.Profile.Proxy.GetProfileValueProxy();
            bpObj.ProfileOrg = UFIDA.U9.Base.Context.LoginOrg.ID;
            bpObj.ProfileCode = "RcvDocTypeCode";
            UFIDA.U9.Base.Profile.PVDTOData pVTDOData = bpObj.Do();
            if (pVTDOData != null && pVTDOData.ProfileValue != "")
            {
                rcvDocTypeCode = pVTDOData.ProfileValue;
            }
           
            return rcvDocTypeCode;
        }
    }
}

	#endregion
	
	